package com.bsoft.gol.editor.dao;


import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import com.bsoft.gol.editor.editor.EditorQuestionnaireTitle;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by Sangs on 2020/4/3.
 */
public abstract class EditorQuestionnaireTitleDAO extends ctd.persistence.support.hibernate.HibernateSupportDelegateDAO<EditorQuestionnaireTitle> {

    public EditorQuestionnaireTitleDAO() {
        this.setKeyField("titleId");
    }

    @DAOMethod(sql = "from EditorQuestionnaireTitle where orderNum >=:orderNum and questionnaireId=:questionnaireId")
    public abstract List<EditorQuestionnaireTitle> queryEditorQuestionnaireTitleByOrderNum(@DAOParam("orderNum") Integer orderNum,@DAOParam("questionnaireId") String questionnaireId);


    @DAOMethod(sql = "from EditorQuestionnaireTitle where orderNum =:orderNum and questionnaireId=:questionnaireId")
    public abstract List<EditorQuestionnaireTitle> queryEditorQuestionnaireTitleByOrderNumLow(@DAOParam("orderNum") Integer orderNum,@DAOParam("questionnaireId") String questionnaireId);

    @DAOMethod(sql = "update EditorQuestionnaireTitle set orderNum=:orderNum,titleNum=:titleNum where titleId =:titleId ")
    public abstract void updateEditorQuestionnaireTitleOrder(@DAOParam("titleId") String titleId,@DAOParam("orderNum") Integer orderNum,@DAOParam("titleNum") Integer titleNum);

    @DAOMethod(sql = "from EditorQuestionnaireTitle where  questionnaireId=:questionnaireId order by orderNum")
    public abstract List<EditorQuestionnaireTitle> queryEditorQuestionnaireTitleByQid(@DAOParam("questionnaireId") String questionnaireId);

    @DAOMethod(sql = "from EditorQuestionnaireTitle where orderNum <:preOrderNum and orderNum >=:afterOrderNum and questionnaireId=:questionnaireId")
    public abstract List<EditorQuestionnaireTitle> queryEditorQuestionnaireTitleByAfterOrderNum(@DAOParam("questionnaireId") String questionnaireId,@DAOParam("afterOrderNum") Integer afterOrderNum,@DAOParam("preOrderNum") Integer preOrderNum);

    @DAOMethod(sql = "from EditorQuestionnaireTitle where orderNum >:preOrderNum and orderNum <=:afterOrderNum and questionnaireId=:questionnaireId")
    public abstract List<EditorQuestionnaireTitle> queryEditorQuestionnaireTitleByPreOrderNumOrderNum(@DAOParam("questionnaireId") String questionnaireId,@DAOParam("afterOrderNum") Integer afterOrderNum,@DAOParam("preOrderNum") Integer preOrderNum);

    @DAOMethod(sql = "update EditorQuestionnaireTitle set displayLogicFlag=:displayLogicFlag where titleId =:titleId ")
    public abstract void updateEditorQuestionnaireTitleFlag(@DAOParam("titleId") String titleId,@DAOParam("displayLogicFlag") Byte displayLogicFlag);

    @DAOMethod(sql = "update EditorQuestionnaireTitle set skipFlag=:skipFlag where titleId =:titleId ")
    public abstract void updateEditorQuestionnaireTitleSkipFlag(@DAOParam("titleId") String titleId,@DAOParam("skipFlag") Byte skipFlag);

    @DAOMethod(sql = "from EditorQuestionnaireTitle where orderNum >:orderNum and questionnaireId=:questionnaireId")
    public abstract List<EditorQuestionnaireTitle> queryEditorQuestionnaireTitleByOrderNumD(@DAOParam("orderNum") Integer orderNum,@DAOParam("questionnaireId") String questionnaireId);

    @DAOMethod(sql = "from EditorQuestionnaireTitle where titleNum >:titleNum and questionnaireId=:questionnaireId")
    public abstract List<EditorQuestionnaireTitle> queryEditorQuestionnaireTitleByTitle(@DAOParam("titleNum") Integer titleNum,@DAOParam("questionnaireId") String questionnaireId);

    @DAOMethod(sql = "update EditorQuestionnaireTitle set titleNum=:titleNum where titleId =:titleId ")
    public abstract void updateEditorQuestionnaireTitleTitleNum(@DAOParam("titleId") String titleId,@DAOParam("titleNum") Integer titleNum);

    @DAOMethod(sql = "from EditorQuestionnaireTitle where titleId in (:titleId) ")
    public abstract List<EditorQuestionnaireTitle> queryEditorQuestionnaireTitleByTitleIds(@DAOParam("titleId") List<String> titleId);



    public Long queryEditorTitleCount(final String questionnaireId) {
        HibernateStatelessResultAction<Long> action = new AbstractHibernateStatelessResultAction<Long>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append("select count(1) countValue from editor_questionnaire_title a where 1=1");
                sqlBuilder.append(" and  questionnaire_id  =  '"+questionnaireId+"'");
                sqlBuilder.append("  order by  order_num desc ");
                Query query = ss.createSQLQuery(sqlBuilder.toString());
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);

                List<Map> dbList = query.list();

                if (CommonUtils.isNotEmpty(dbList)) {
                    setResult(Long.valueOf(String.valueOf(dbList.get(0).get("countValue"))));
                } else {
                    setResult(0L);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

}